
$(document).ready(function(){
	var debug = false;
	if (typeof (console) == 'object') {
		debug = true;
	}
	function gen() {
		var fl = familyNames.length;
		var cl = commonNames.length;
		var pos = Math.min(parseInt(Math.random() * fl), fl);
		//alert(pos);

		var f = familyNames[pos];
		if (debug) {
			console.log("select pos=" + pos
					+ " from familyNames =" + f);
		}
		pos = Math.min(parseInt(Math.random() * cl), cl);
		var n1 = commonNames[pos];
		if (debug) {
			console.log("select pos=" + pos
					+ " from commonNames =" + n1);
		}
		pos = Math.min(parseInt(Math.random() * cl), cl);
		var n2 = commonNames[pos];
		if (debug) {
			console.log("select pos=" + pos
					+ " from commonNames =" + n2);
		}
		var name = "";

		//console.log(f)
		if (Math.random() > 0.3) {//控制单名的数量
			name = f + n1 + n2;
		} else {
			name = f + n1;
		}
		//alert(name)
		return name;
	}
//	生成行政区划代码
	function genCode(limit) {
		var len = areaCodeArray.length;
		var str = "00";
		var code = null;

		function outOfLimit(c, limit) {
			var inA = false;
			if (limit == "*")
				inA = true;
			if (c.indexOf(limit) == 0)
				inA = true;
			return !inA;
		}

		while (str.indexOf("00") > -1 || outOfLimit(str, limit)) {
			var pos = Math.min(parseInt(Math.random() * len),
					len - 1);
			code = areaCodeArray[pos];
			str = code[0];
		}

		if (debug) {
			console.log('gen area code:' + str);
		}

		return str;
	}

	function genIdCard(code) {
		code = areaCode[code];
		var today = new Date();
		var maxAge = 108;
		var from = code[2];
		var to = code[3];
		if (to == "")
			to = today;
		else
			to = new Date(to);
		if (from == "1949-10-1") {
			from = new Date(1900, 0, 1);
		} else {
			from = new Date(from);
		}
		//产生一个生日并使得生日落在一个和行政区划匹配的范围内。当然这也不是绝对准确，人的办事效率不能想象。
		var age = Math.min(parseInt(Math.random() * maxAge),maxAge);
		if (debug) {
			console.log('age:', age);
		}
		var year = today.getFullYear() - age;
		var month = parseInt(Math.random() * 11);
		var day = parseInt(Math.random() * 31);
		var birth = new Date(year, month, day);
		var dateStr = birth.format("yyyymmdd");
		if (debug) {
			console.log('birthday:', year, month, day, dateStr);
		}

		//产生身份证号码
		var arrVerifyCode = [ 1, 0, "x", 9, 8, 7, 6, 5, 4, 3, 2 ];
		var Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5,
		           8, 4, 2 ];

		Ai = code[0]
		+ dateStr
		+ String(parseInt(Math.random() * 999)).lpad(
				"0", 3).toString();

		if (debug) {
			console.log('concat:', Ai);
		}
		for ( var i = 0, ret = 0; i < 17; i++){
			ret += Ai.charAt(i) * Wi[i];
		}
		Ai = Ai.concat(arrVerifyCode[ret %= 11]);
		if (debug) {
			console.log('append verfy code', Ai);
		}
		return Ai;
	}

	function getLocationStr(code) {
		var arr = Devit.util.code.buildPath1(code, 2);
		var s = "";
		for ( var i = 0; i < arr.length; i++) {

			s = s.concat(areaCode[arr[i]][1], " ");
		}
		return s;
	}
	/**
	* limitArea 输入区划的开头数字，不限制请输入*号
	* count 生成数据的数量
	*
	*/
	function genIdCardData(limitArea,count){
		if (limitArea == "*" || ss in areaCode) {
		} else {
			alert("限制区域参数错误，没有这个区划，请输入区划的开头数字，不限制请输入*号");
			return;
		}
		var result = [];
		try {
			for ( var i = 0; i < count; i++) {
				if (debug) {
					console.log('gen ' + i + ' names');
				}
				var code = genCode(limitArea);
				result.push({'name':gen(),'idcard':genIdCard(code),'area':getLocationStr(code)});
			}
		} catch (e) {
			alert(e);
		}
	}
});